package com.xumt.hwHMSdemo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.xumt.hwHMSdemo.cannectSb.HttpRequest;
import com.xumt.hwHMSdemo.ui.ShopingFragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Commodity_message extends AppCompatActivity {
    private String findby_id;
    public static final String EXTRA_ID="ID";
    private String result;
    private String objectname;
    private double objectprice;
private int resId;
    private String image1;
    private int stock;
    private int commodityid;
    private SimpleAdapter evaluateadapter = null;
    private String evaluateresult=null;
    public  static List<Map<String, Object>> evaluatelist = new ArrayList<Map<String, Object>>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_message);
        evaluatelist.clear();

        Map<String, Object> map = CommodityActivity.commoditylist.get(CommodityActivity.position);
        objectname = (String) map.get("commodityname");
        objectprice = Double.valueOf((String) map.get("commodityprice"));
        image1 = (String) map.get("commodityimage");
        commodityid = (int) map.get("commodityid");
        stock = (int) map.get("stock");
        TextView textView = findViewById(R.id.name);
        textView.setText(CommodityActivity.clickname);

        TextView textView1 = findViewById(R.id.description);
        textView1.setText((String) map.get("commoditymessage"));

        TextView textView3 = findViewById(R.id.price);
        textView3.setText((String) map.get("commodityprice"));

        Context ctx = getBaseContext();
        resId = getResources().getIdentifier((String) map.get("commodityimage"), "drawable", ctx.getPackageName());
        ImageView photo = findViewById(R.id.image);
        photo.setImageResource(resId);
        Button button = (Button) findViewById(R.id.addbutton);
        Button commit=(Button)findViewById(R.id.commit);



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
                //  addshoppingcart(objectname,objectprice,image1);
//                Toast.makeText(Commodity_message.this,"Add success",Toast.LENGTH_SHORT).show();
            }
        });
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               showDialog1();

            }
        });
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText=(EditText)findViewById(R.id.pinglun);
                String evalute=editText.getText().toString();
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("username",Login2.usernamen  );
                map.put("evaluate", evalute);
                map.put("commodityid", commodityid);
                Context ctx=getBaseContext();
                int resId = getResources().getIdentifier("logo", "drawable" ,ctx.getPackageName());
                map.put("image",resId);
                evaluatelist.add(map);
                evaluateadapter.notifyDataSetChanged();
                addevaluate(Login2.usernamen,evalute,commodityid);
                Toast.makeText(Commodity_message.this,"发表评论成功",Toast.LENGTH_SHORT).show();
                editText.setText("");

            }
        });
        ListView listView = (ListView) findViewById(R.id.evaluatelist);
        evaluateadapter = new MySimpleAdapter(this, getData(), R.layout.evaluatelist, new String[]{"username", "evaluate", "image"},
                new int[]{R.id.username, R.id.evaluate, R.id.userimage});

        listView.setAdapter(evaluateadapter);

    }

//    public void findby_id(int id)
//    {
//
//        HttpRequest request = new HttpRequest();
//        new Thread() {
//            public void run() {
//                try {
//                    findby_id=request.sendGet("http://169.254.67.221:8081/Commdity/findby_id","id="+id);
//
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }.start();
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }

    private class MySimpleAdapter extends SimpleAdapter {

        public MySimpleAdapter(Context context,
                               List<? extends Map<String, ?>> data, int resource,
                               String[] from, int[] to) {
            super(context, data, resource, from, to);
            // TODO Auto-generated constructor stub
        }
    }

    public void addevaluate(String username,String evaulate,int commodityid)
    {
        HttpRequest request = new HttpRequest();
        new Thread() {
            public void run() {
                try {
                    request.sendGet("http://169.254.67.221:8081/Evaluate/addevaluate","username="+username + "&" +"evaluate="+evaulate + "&" +"commodityid="+commodityid);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void getevaluate()
    {

        HttpRequest request = new HttpRequest();
        new Thread() {
            public void run() {
                try {
                    Map<String, Object> map=CommodityActivity.commoditylist.get(CommodityActivity.position);
                    commodityid=(int)map.get("commodityid");
                    evaluateresult=request.sendGet("http://169.254.67.221:8081/Evaluate/getevaluate","commodityid="+commodityid);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void addshoppingcart(int number)
    {
        HttpRequest request = new HttpRequest();
        new Thread() {
            public void run() {
                try {
                    request.sendGet("http://169.254.67.221:8081/shop/addtocard", "username=" + Login2.usernamen + "&" +"shopid="+ ShopingFragment.clickshop+"&"+
                            "commodityid=" + commodityid + "&" +"number="+number);


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private List<Map<String, Object>> getData() {
        try {
            getevaluate();
            Thread.sleep(500);
            JSONArray jsonArray = new JSONArray(evaluateresult);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("username",  jsonObject.getString("username"));
                map.put("evaluate", jsonObject.getString("evaluate"));
//                map.put("id", jsonObject.getString("id"));
//                map.put("classification", jsonObject.getString("classification"));
//                map.put("commodityimage", jsonObject.getString("image"));
                map.put("commodityid",  jsonObject.getInt("commodityid"));

               Context ctx=getBaseContext();
               int resId = getResources().getIdentifier("logo", "drawable" ,ctx.getPackageName());
               map.put("image",resId);
                evaluatelist.add(map);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return evaluatelist;
    }

    private  void showDialog1(){


        View view = LayoutInflater.from(this).inflate(R.layout.imagelook,null,false);
        final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create();

        ImageView photo1 = view.findViewById(R.id.thisimage1);
        photo1.setImageResource(resId);
        dialog.show();
    }
    private void showDialog(){
        View view = LayoutInflater.from(this).inflate(R.layout.dialog1,null,false);
        final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create();

        Button delete = view.findViewById(R.id.dialogdelete);
        Button add = view.findViewById(R.id.dialogadd);
        Button commit=view.findViewById(R.id.commit);
        TextView number=view.findViewById(R.id.number);
        TextView selectnumber=view.findViewById(R.id.selectnumber);
        selectnumber.setText("请选择商品数量"+"("+stock+")");
        int i=Integer.parseInt(number.getText().toString());
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i=Integer.parseInt(number.getText().toString());
               if(i>1)i--;
               number.setText(String.valueOf(i));
                //... To-do

            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //... To-do
                int i=Integer.parseInt(number.getText().toString());
                if(i<stock)i++;
                number.setText(String.valueOf(i));
            }
        });

        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //... To-do
                int i=Integer.parseInt(number.getText().toString());
                addshoppingcart(i);
                Toast.makeText(Commodity_message.this,"添加购物车成功",Toast.LENGTH_SHORT).show();
               dialog.dismiss();
            }
        });
        dialog.show();

    }


}